package com.hk.controller;

import com.hk.Enume.OperationType;
import com.hk.annotation.AotoFile;
import com.hk.constant.LogConstaant;
import com.hk.context.BaseContext;
import com.hk.core.R;
import com.hk.core.Result;
import com.hk.core.UserUtlis;
import com.hk.core.basePageResult.BasePageResult;
import com.hk.domain.Contract;
import com.hk.dto.ContractChangeDto;
import com.hk.dto.ContractDto;
import com.hk.mapper.ContractMapper;
import com.hk.service.ContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * 合同信息
 */

@RestController
@RequestMapping("/contract")
@Slf4j
public class ContractController {

    @Autowired
    private ContractService contractService;
    @Autowired
    private ContractMapper  contractMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 获取合同列表(分页 + 条件查询)
     * @param contractDto
     * @return
     */
    @GetMapping("/list")
    public BasePageResult getList(ContractDto contractDto) {
         return contractService.getList(contractDto);
    }

    /**
     * 根据合同id查询合同信息（页面回显）
     * @param id
     * @return
     */
    @GetMapping("detail/{id}")
    public Result  getByContractId(@PathVariable String id) {
        return Result.success(contractMapper.selectById(id)) ;
    }

    /**
     * 修改合同信息
     * @param contract
     * @return
     */
    @AotoFile(value = OperationType.PUT,title = LogConstaant.CONTRACT_LOG)
    @PutMapping
    public Result updateByContractId(@RequestBody Contract contract) {
        return Result.success(contractService.updateById(contract));
    }


    /**
     * 添加合同信息
     * @param contract
     * @return
     */
    @AotoFile(value = OperationType.POST,title = LogConstaant.CONTRACT_LOG)
    @PostMapping
    public Result addContract(@RequestBody Contract contract){
        //设置合同id，合同id为八位数，用uuid截取八位数，因为合同数较少，基本不可能重复，就没做碰撞测试
        String contractID = UUID.randomUUID().toString().substring(0,8);
        contract.setId(contractID);
        //从redis中获取url
        Integer userId = BaseContext.getCurrent().getUserId();
        Object url = redisTemplate.opsForValue().get(userId.toString());
        log.info("文件路径为：{}",url);
        contract.setFileName(url.toString());
        return Result.success(contractService.save(contract));
    }

    /**
     * 获取合同详细信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getContractInfoById(@PathVariable String id){
        return Result.success(contractService.getContractInfoById(id));
    }

    /**
     * 商机转合同
     */
    @RestController
    @RequestMapping("/contract")
    public class ContractControllerTest {
        @Autowired
        ContractService contractTestService;
            @PutMapping("/changeContract/{id}")
            public R change(@RequestBody ContractChangeDto dto, @PathVariable Integer id){
                contractTestService.change(dto,id);
                  return R.ok();
               }
             }


}
